<template>
  <div>
    <a-row type="flex" :gutter="10">
      <a-col :span="12" style="height: calc(100vh - 125px)">
        <a-card :bordered="false" style="min-height: calc(100vh - 125px)">
          <!-- 条件搜索 -->
          <div class="table-page-search-wrapper">
            <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
              <a-row :gutter="32">
                <a-col :span="12">
                  <a-form-item label="帖子状态">
                    <a-select placeholder="请选择帖子状态" v-model="queryParam.status" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue" @click="handleQuery">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced" v-hasPermi="['platform:region:add']">
                  <a-form-item label="用户">
                    <select-user v-model="queryParam.userId" select-model="single" placeholder="请选择用户" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced" v-hasPermi="['platform:region:add']">
                  <a-form-item label="地区">
                    <select-region v-model="queryParam.regionId" placeholder="请选择地区" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="评论">
                    <select-comment v-model="queryParam.pid" placeholder="请选择评论" status="1,4"></select-comment>
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="帖子">
                    <select-post v-model="queryParam.postId" placeholder="请选择帖子" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="评论内容" v-if="advanced">
                    <a-input v-model="queryParam.content" placeholder="请输入评论内容" allow-clear @keyup.enter.native="handleQuery" />
                  </a-form-item>
                </a-col>
                <a-col :span="12" v-if="advanced">
                  <a-form-item label="子节点">
                    <a-select placeholder="请选择帖子状态" v-model="queryParam.isChild" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in isChildOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col>
                  <span class="table-page-search-submitButtons" style="float: right">
                    <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                    <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                      {{ advanced ? '收起' : '展开' }}
                      <a-icon :type="advanced ? 'up' : 'down'" />
                    </a>
                  </span>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <a-table
            :pagination="{
              current: queryParam.pageNum,
              pageSize: queryParam.pageSize,
              total: total,
              showSizeChanger: true,
              showLessItems: true,
              showQuickJumper: true,
              showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
              onChange: changeSize,
              onShowSizeChange: onShowSizeChange
            }"
            tableKey="base-comment-index-table"
            @change="handleTableChange"
            rowKey="id"
            size="middle"
            @refresh="getList"
            :columns="columns"
            :data-source="commentList"
            :loading="loading"
            :format-conditions="true"
            :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
            :scroll="{ x: 1500 }"
          >
            <span slot="status" slot-scope="text, record">
              <a-badge :status="statusBage(record.status)" :text="statusFormat(record)" />
            </span>
            <span slot="isChild" slot-scope="text, record">
              {{ isChildFormat(record) }}
            </span>
          </a-table>
        </a-card>
      </a-col>
      <a-col :span="12" style="height: calc(100vh - 125px); overflow-y: auto">
        <comment-detail :commentId="id" @getList="getList" :statusOptions="statusOptions" :isChildOptions="isChildOptions" />
      </a-col>
    </a-row>
  </div>
</template>

<script>
import { listComment, getInitData } from '@/api/platform/comment'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectRegion from '@/views/platform/components/SelectRegion'
import SelectPost from '@/views/platform/components/SelectPost'
import SelectComment from '@/views/platform/components/SelectComment'
import CommentDetail from './modules/CommentDetail'
import CommentApprove from './modules/CommentApprove'
export default {
  name: 'CommentApprove',
  components: {
    SelectUser,
    SelectPost,
    SelectComment,
    SelectRegion,
    CommentDetail
  },
  data() {
    return {
      open: false,
      // 遮罩层
      loading: true,
      // 选中
      id: undefined,
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中的主键集合
      selectedRowKeys: [],
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 10 },
      // 内容区域的百分比
      wrapperCol: { span: 14 },
      // 评论管理表格数据
      commentList: [],
      // 评论状态字典
      statusOptions: [],
      isChildOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        pid: undefined,
        userId: undefined,
        regionId: undefined,
        postId: undefined,
        content: undefined,
        isChild: undefined,
        status: '1'
      },
      columns: [
        {
          title: '用户',
          dataIndex: 'userName',
          ellipsis: true,
          align: 'left',
          width: '100px'
        },
        {
          title: '地区',
          dataIndex: 'regionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '版块',
          dataIndex: 'sectionName',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '帖子标题',
          dataIndex: 'title',
          ellipsis: true,
          align: 'left',
          width: '180px'
        },
        {
          title: '评论',
          dataIndex: 'previousContent',
          ellipsis: true,
          align: 'left',
          width: '120px'
        },
        {
          title: '评论内容',
          dataIndex: 'content',
          ellipsis: true,
          align: 'left',
          width: '250px'
        },
        {
          title: '是否有子节点',
          dataIndex: 'isChild',
          scopedSlots: { customRender: 'isChild' },
          ellipsis: true,
          align: 'left',
          width: '95px'
        },
        {
          title: '评论发布时间',
          dataIndex: 'createTime',
          ellipsis: true,
          align: 'left',
          width: '150px'
        },
        {
          title: '回复数',
          dataIndex: 'replyNumber',
          align: 'right',
          width: '80px'
        },
        {
          title: '点赞数',
          dataIndex: 'likeNumber',
          align: 'right',
          width: '80px'
        },
        {
          title: '举报数',
          dataIndex: 'reportNumber',
          align: 'right',
          width: '80px'
        },
        {
          title: '评论状态',
          dataIndex: 'status',
          ellipsis: true,
          align: 'center',
          width: '120px',
          scopedSlots: { customRender: 'status' },
          fixed: 'right'
        }
      ]
    }
  },
  created() {
    this.getList()
    getInitData('approve_status,is_child').then(response => {
      this.statusOptions = response.data.approve_status
      this.isChildOptions = response.data.is_child
    })
  },
  methods: {
    ...CommentApprove.methods,
    /** 查询评论管理列表 */
    getList() {
      this.loading = true
      this.id = undefined
      this.selectedRowKeys = []
      const param = this.queryParam
      if (this.queryParam.userId) {
        param.userId = this.queryParam.userId.ids
      }
      if (this.queryParam.pid) {
        param.pid = this.queryParam.pid.commentId
      }
      listComment(param).then(response => {
        this.commentList = response.data.rows
        this.total = response.data.total
        if (response.data.rows.length > 0) {
          this.selectedRowKeys.push(this.commentList[0].id)
          this.id = this.commentList[0].id
        }
        this.loading = false
      })
    },
    //  评论状态徽标数类型
    statusBage(status) {
      if (status === '0') {
        return 'default'
      } else if (status === '1') {
        return 'processing'
      } else if (status === '2') {
        return 'warning'
      } else if (status === '3') {
        return 'error'
      } else if (status === '4') {
        return 'processing'
      } else if (status === '5') {
        return 'error'
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        pid: undefined,
        postId: undefined,
        userId: undefined,
        content: undefined,
        isChild: undefined,
        status: '1'
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.id = this.selectedRows[0].id
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
